home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8686 / FAQ.004
Encoding:
Text File  |  1996-08-05  |  34.1 KB  |  841 lines

  1. Path: news1.h1.usa.pipeline.com!psinntp!psinntp!howland.reston.ans.net!gatech!news.mathworks.com!news.kei.com!newsstand.cit.cornell.edu!ub!library.erc.clarkson.edu!sun.soe.clarkson.edu!cline
  2. From: cline@sun.soe.clarkson.edu (Marshall Cline)
  3. Newsgroups: comp.lang.c++
  4. Subject: C++ FAQ: posting #4/4
  5. Followup-To: comp.lang.c++
  6. Date: 1 Feb 1996 03:20:47 GMT
  7. Organization: Paradigm Shift, Inc (technology consulting)
  8. Lines: 824
  9. Sender: cline@sun.soe.clarkson.edu
  10. Distribution: world
  11. Expires: +1 month
  12. Message-ID: <4epbif$bpv@library.erc.clarkson.edu>
  13. Reply-To: cline@parashift.com (Marshall Cline)
  14. NNTP-Posting-Host: sun.soe.clarkson.edu
  15. Summary: Please read this before posting to comp.lang.c++
  16.  
  17. comp.lang.c++ Frequently Asked Questions list (with answers, fortunately).
  18. Copyright (C) 1991-96 Marshall P. Cline, Ph.D.
  19. Posting 4 of 4.
  20. Posting #1 explains copying permissions, (no)warranty, table-of-contents, etc
  21.  
  22. ==============================================================================
  23. SECTION 17: Linkage-to/relationship-with C
  24. ==============================================================================
  25.  
  26. Q105: How can I call a C function "f(int,char,float)" from C++ code?
  27.  
  28. Tell the C++ compiler that it is a C function:
  29.     extern "C" void f(int,char,float);
  30.  
  31. Be sure to include the full function prototype.  A block of many C functions
  32. can be grouped via braces, as in:
  33.  
  34.     extern "C" {
  35.       void* malloc(size_t);
  36.       char* strcpy(char* dest, const char* src);
  37.       int   printf(const char* fmt, ...);
  38.     }
  39.  
  40. ==============================================================================
  41.  
  42. Q106: How can I create a C++ function "f(int,char,float)" that is callable by
  43.    my C code?
  44.  
  45. The C++ compiler must know that "f(int,char,float)" is to be called by a C
  46. compiler using the same "extern C" construct detailed in the previous FAQ.
  47. Then you define the function in your C++ module:
  48.  
  49.     void f(int x, char y, float z)
  50.     {
  51.       //...
  52.     }
  53.  
  54. The "extern C" line tells the compiler that the external information sent to
  55. the linker should use C calling conventions and name mangling (e.g., preceded
  56. by a single underscore).  Since name overloading isn't supported by C, you
  57. can't make several overloaded fns simultaneously callable by a C program.
  58.  
  59. Caveats and implementation dependencies:
  60.  * your "main()" should be compiled with your C++ compiler (for static init).
  61.  * your C++ compiler should direct the linking process (for special libraries).
  62.  * your C and C++ compilers may need to come from same vendor and have
  63.    compatible versions (i.e., needs same calling convention, etc.).
  64.  
  65. ==============================================================================
  66.  
  67. Q107: Why's the linker giving errors for C/C++ fns being called from C++/C
  68.    fns?
  69.  
  70. See the previous two FAQs on how to use "extern "C"."
  71.  
  72. ==============================================================================
  73.  
  74. Q108: How can I pass an object of a C++ class to/from a C function?
  75.  
  76. Here's an example:
  77.  
  78.     /****** C/C++ header file: Fred.h ******/
  79.     #ifdef __cplusplus    /*"__cplusplus" is #defined if/only-if compiler is C++*/
  80.       extern "C" {
  81.     #endif
  82.  
  83.     #ifdef __STDC__
  84.       extern void c_fn(struct Fred*);    /* ANSI-C prototypes */
  85.       extern struct Fred* cplusplus_callback_fn(struct Fred*);
  86.     #else
  87.       extern void c_fn();            /* K&R style */
  88.       extern struct Fred* cplusplus_callback_fn();
  89.     #endif
  90.  
  91.     #ifdef __cplusplus
  92.       }
  93.     #endif
  94.  
  95.     #ifdef __cplusplus
  96.       class Fred {
  97.       public:
  98.         Fred();
  99.         void wilma(int);
  100.       private:
  101.         int a_;
  102.       };
  103.     #endif
  104.  
  105. "Fred.C" would be a C++ module:
  106.  
  107.     #include "Fred.h"
  108.     Fred::Fred() : a_(0) { }
  109.     void Fred::wilma(int a) : a_(a) { }
  110.  
  111.     Fred* cplusplus_callback_fn(Fred* fred)
  112.     {
  113.       fred->wilma(123);
  114.       return fred;
  115.     }
  116.  
  117. "main.C" would be a C++ module:
  118.  
  119.     #include "Fred.h"
  120.  
  121.     int main()
  122.     {
  123.       Fred fred;
  124.       c_fn(&fred);
  125.       return 0;
  126.     }
  127.  
  128. "c-fn.c" would be a C module:
  129.  
  130.     #include "Fred.h"
  131.     void c_fn(struct Fred* fred)
  132.     {
  133.       cplusplus_callback_fn(fred);
  134.     }
  135.  
  136. Passing ptrs to C++ objects to/from C fns will FAIL if you pass and get back
  137. something that isn't EXACTLY the same pointer.  For example, DON'T pass a base
  138. class ptr and receive back a derived class ptr, since your C compiler won't
  139. understand the pointer conversions necessary to handle multiple and/or virtual
  140. inheritance.
  141.  
  142. ==============================================================================
  143.  
  144. Q109: Can my C function access data in an object of a C++ class?
  145.  
  146. Sometimes.
  147.  
  148. (First read the previous FAQ on passing C++ objects to/from C functions.)
  149.  
  150. You can safely access a C++ object's data from a C function if the C++ class:
  151.  * has no virtual functions (including inherited virtual fns)
  152.  * has all its data in the same access-level section (private/protected/public)
  153.  * has no fully-contained subobjects with virtual fns
  154.  
  155. If the C++ class has any base classes at all (or if any fully contained
  156. subobjects have base classes), accessing the data will TECHNICALLY be
  157. non-portable, since class layout under inheritance isn't imposed by the
  158. language.  However in practice, all C++ compilers do it the same way: the base
  159. class object appears first (in left-to-right order in the event of multiple
  160. inheritance), and subobjects follow.
  161.  
  162. Furthermore, if the class (or any base class) contains any virtual functions,
  163. you can often (but less than always) assume a "void*" appears in the object
  164. either at the location of the first virtual function or as the first word in
  165. the object.  Again, this is not required by the language, but it is the way
  166. "everyone" does it.
  167.  
  168. If the class has any virtual base classes, it is even more complicated and less
  169. portable.  One common implementation technique is for objects to contain an
  170. object of the virtual base class (V) last (regardless of where "V" shows up as
  171. a virtual base class in the inheritance hierarchy).  The rest of the object's
  172. parts appear in the normal order.  Every derived class that has V as a virtual
  173. base class actually has a POINTER to the V part of the final object.
  174.  
  175. ==============================================================================
  176.  
  177. Q110: Why do I feel like I'm "further from the machine" in C++ as opposed to
  178.    C?
  179.  
  180. Because you are.
  181.  
  182. As an OOPL, C++ allows you to model the problem domain itself, which allows you
  183. to program in the language of the problem domain rather than in the language of
  184. the solution domain.
  185.  
  186. One of C's great strengths is the fact that it has "no hidden mechanism": what
  187. you see is what you get.  You can read a C program and "see" every clock cycle.
  188. This is not the case in C++; old line C programmers (such as many of us once
  189. were) are often ambivalent (can anyone say, "hostile") about this feature, but
  190. they soon realize that it provides a level of abstraction and economy of
  191. expression which lowers maintenance costs without destroying run-time
  192. performance.
  193.  
  194. Naturally you can write bad code in any language; C++ doesn't guarantee any
  195. particular level of quality, reusability, abstraction, or any other measure of
  196. "goodness."  C++ doesn't try to make it impossible for bad programmers to write
  197. bad programs; it enables reasonable developers to create superior software.
  198.  
  199. ==============================================================================
  200. SECTION 18: Pointers to member functions
  201. ==============================================================================
  202.  
  203. Q111: Is the type of "ptr-to-member-fn" different from "ptr-to-fn"?
  204.  
  205. Yep.
  206.  
  207. Consider the following function:
  208.  
  209.     int f(char a, float b);
  210.  
  211. If this is an ordinary function, its type is:    int (*)      (char,float);
  212. If this is a method of class Fred, its type is:  int (Fred::*)(char,float);
  213.  
  214. ==============================================================================
  215.  
  216. Q112: How do I pass a ptr to member fn to a signal handler, X event callback,
  217.    etc?
  218.  
  219. Don't.
  220.  
  221. Because a member function is meaningless without an object to invoke it on, you
  222. can't do this directly (if The X Windows System was rewritten in C++, it would
  223. probably pass references to OBJECTS around, not just pointers to fns; naturally
  224. the objects would embody the required function and probably a whole lot more).
  225.  
  226. As a patch for existing software, use a top-level (non-member) function as a
  227. wrapper which takes an object obtained through some other technique (held in a
  228. global, perhaps).  The top-level function would apply the desired member
  229. function against the global object.
  230.  
  231. E.g., suppose you want to call Fred::memfn() on interrupt:
  232.  
  233.     class Fred {
  234.     public:
  235.       void memfn();
  236.       static void staticmemfn();    //a static member fn can handle it
  237.       //...
  238.     };
  239.  
  240.     //wrapper fn remembers the object on which to invoke memfn in a global:
  241.     Fred* object_which_will_handle_signal;
  242.     void Fred_memfn_wrapper() { object_which_will_handle_signal->memfn(); }
  243.  
  244.     main()
  245.     {
  246.       /* signal(SIGINT, Fred::memfn); */   //Can NOT do this
  247.       signal(SIGINT, Fred_memfn_wrapper);  //Ok
  248.       signal(SIGINT, Fred::staticmemfn);   //Also Ok
  249.     }
  250.  
  251. Note: static member functions do not require an actual object to be invoked, so
  252. ptrs-to-static-member-fns are type compatible with regular ptrs-to-fns (see ARM
  253. ["Annotated Reference Manual"] p.25, 158).
  254.  
  255. ==============================================================================
  256.  
  257. Q113: Why do I keep getting compile errors (type mismatch) when I try to use a
  258.    member function as an interrupt service routine? 
  259.  
  260. This is a special case of the previous two questions, therefore read the
  261. previous two answers first.
  262.  
  263. Non-static member functions have a hidden parameter that corresponds to the
  264. 'this' pointer.  The 'this' pointer points to the instance data for the
  265. object.  The interrupt hardware/firmware in the system is not capable of
  266. providing the 'this' pointer argument.  You must use "normal" functions (non
  267. class members) or static member functions as interrupt service routines.
  268.  
  269. One possible solution is to use a static member as the interrupt service
  270. routine and have that function look somewhere to find the instance/member pair
  271. that should be called on interrupt.  Thus the effect is that a normal method
  272. is invoked on an interrupt, but for technical reasons you need to call an
  273. intermediate function first.
  274.  
  275. ==============================================================================
  276.  
  277. Q114: Why am I having trouble taking the address of a C++ function?
  278.  
  279. This is a corollary to the previous FAQ.
  280.  
  281. Long answer: In C++, member fns have an implicit parameter which points to the
  282. object (the "this" ptr inside the member fn).  Normal C fns can be thought of
  283. as having a different calling convention from member fns, so the types of their
  284. ptrs (ptr-to-member-fn vs ptr-to-fn) are different and incompatible.  C++
  285. introduces a new type of ptr, called a ptr-to-member, which can be invoked only
  286. by providing an object (see ARM ["Annotated Reference Manual"] 5.5).
  287.  
  288. NOTE: do NOT attempt to "cast" a ptr-to-mem-fn into a ptr-to-fn; the result is
  289. undefined and probably disastrous.  E.g., a ptr-to- member-fn is NOT required
  290. to contain the machine addr of the appropriate fn (see ARM, 8.1.2c, p.158).  As
  291. was said in the last example, if you have a ptr to a regular C fn, use either a
  292. top-level (non-member) fn, or a "static" (class) member fn.
  293.  
  294. ==============================================================================
  295.  
  296. Q115: How do I declare an array of pointers to member functions?
  297.  
  298. Keep your sanity with "typedef".
  299.  
  300.     class Fred {
  301.     public:
  302.       int f(char x, float y);
  303.       int g(char x, float y);
  304.       int h(char x, float y);
  305.       int i(char x, float y);
  306.       //...
  307.     };
  308.  
  309.     typedef  int (Fred::*FredPtr)(char x, float y);
  310.  
  311. Here's the array of pointers to member functions:
  312.  
  313.     FredPtr a[4] = { &Fred::f, &Fred::g, &Fred::h, &Fred::i };
  314.  
  315. To call one of the member functions on object "fred":
  316.  
  317.     void userCode(Fred& fred, int methodNum, char x, float y)
  318.     {
  319.       //assume "methodNum" is between 0 and 3 inclusive
  320.       (fred.*a[methodNum])(x, y);
  321.     }
  322.  
  323. You can make the call somewhat clearer using a #define:
  324.  
  325.     #define  callMethod(object,ptrToMethod)   ((object).*(ptrToMethod))
  326.     callMethod(fred, a[methodNum]) (x, y);
  327.  
  328. ==============================================================================
  329. SECTION 19: Container classes and templates
  330. ==============================================================================
  331.  
  332. Q116: How can I insert/access/change elements from a linked
  333.    list/hashtable/etc?
  334.  
  335. I'll use an "inserting into a linked list" as a prototypical example.  It's easy
  336. to allow insertion at the head and tail of the list, but limiting ourselves to
  337. these would produce a library that is too weak (a weak library is almost worse
  338. than no library).
  339.  
  340. This answer will be a lot to swallow for novice C++'ers, so I'll give a couple
  341. of options.  The first option is easiest; the second and third are better.
  342.  
  343. [1] Empower the "List" with a "current location," and methods such as
  344. advance(), backup(), atEnd(), atBegin(), getCurrElem(), setCurrElem(Elem),
  345. insertElem(Elem), and removeElem().  Although this works in small examples, the
  346. notion of "a" current position makes it difficult to access elements at two or
  347. more positions within the List (e.g., "for all pairs x,y do the following...").
  348.  
  349. [2] Remove the above methods from the List itself, and move them to a separate
  350. class, "ListPosition."  ListPosition would act as a "current position" within a
  351. List.  This allows multiple positions within the same List.  ListPosition would
  352. be a friend of List, so List can hide its innards from the outside world (else
  353. the innards of List would have to be publicized via public methods in List).
  354. Note: ListPosition can use operator overloading for things like advance() and
  355. backup(), since operator overloading is syntactic sugar for normal methods.
  356.  
  357. [3] Consider the entire iteration as an atomic event, and create a class
  358. template to embodies this event.  This enhances performance by allowing the
  359. public access methods (which may be virtual fns) to be avoided during the inner
  360. loop.  Unfortunately you get extra object code in the application, since
  361. templates gain speed by duplicating code.  For more, see [Koenig, "Templates as
  362. interfaces," JOOP, 4, 5 (Sept 91)], and [Stroustrup, "The C++ Programming
  363. Language Second Edition," under "Comparator"].
  364.  
  365. ==============================================================================
  366.  
  367. Q117: What's the idea behind "templates"?
  368.  
  369. A template is a cookie-cutter that specifies how to cut cookies that all look
  370. pretty much the same (although the cookies can be made of various kinds of
  371. dough, they'll all have the same basic shape).  In the same way, a class
  372. template is a cookie cutter to description of how to build a family of classes
  373. that all look basically the same, and a function template describes how to
  374. build a family of similar looking functions.
  375.  
  376. Class templates are often used to build type safe containers (although this
  377. only scratches the surface for how they can be used).
  378.  
  379. ==============================================================================
  380.  
  381. Q118: What's the syntax / semantics for a "function template"?
  382.  
  383. Consider this function that swaps its two integer arguments:
  384.  
  385.     void swap(int& x, int& y)
  386.     {
  387.       int tmp = x;
  388.       x = y;
  389.       y = tmp;
  390.     }
  391.  
  392. If we also had to swap floats, longs, Strings, Sets, and FileSystems, we'd get
  393. pretty tired of coding lines that look almost identical except for the type.
  394. Mindless repetition is an ideal job for a computer, hence a function template:
  395.  
  396.     template<class T>
  397.     void swap(T& x, T& y)
  398.     {
  399.       T tmp = x;
  400.       x = y;
  401.       y = tmp;
  402.     }
  403.  
  404. Every time we used "swap()" with a given pair of types, the compiler will go to
  405. the above definition and will create yet another "template function" as an
  406. instantiation of the above.  E.g.,
  407.  
  408.     main()
  409.     {
  410.       int    i,j;  /*...*/  swap(i,j);  //instantiates a swap for "int"
  411.       float  a,b;  /*...*/  swap(a,b);  //instantiates a swap for "float"
  412.       char   c,d;  /*...*/  swap(c,d);  //instantiates a swap for "char"
  413.       String s,t;  /*...*/  swap(s,t);  //instantiates a swap for "String"
  414.     }
  415.  
  416. (note: a "template function" is the instantiation of a "function template").
  417.  
  418. ==============================================================================
  419.  
  420. Q119: What's the syntax / semantics for a "class template"?
  421.  
  422. Consider a container class of that acts like an array of integers:
  423.  
  424.     //this would go into a header file such as "Array.h"
  425.     class Array {
  426.     public:
  427.       Array(int len=10)                  : len_(len), data_(new int[len]){}
  428.      ~Array()                            { delete [] data_; }
  429.       int len() const                    { return len_;     }
  430.       const int& operator[](int i) const { data_[check(i)]; }
  431.             int& operator[](int i)       { data_[check(i)]; }
  432.       Array(const Array&);
  433.       Array& operator= (const Array&);
  434.     private:
  435.       int  len_;
  436.       int* data_;
  437.       int  check(int i) const
  438.         { if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_);
  439.           return i; }
  440.     };
  441.  
  442. Just as with "swap()" above, repeating the above over and over for Array of
  443. float, of char, of String, of Array-of-String, etc, will become tedious.
  444.  
  445.     //this would go into a header file such as "Array.h"
  446.     template<class T>
  447.     class Array {
  448.     public:
  449.       Array(int len=10)                : len_(len), data_(new T[len]) { }
  450.      ~Array()                          { delete [] data_; }
  451.       int len() const                  { return len_;     }
  452.       const T& operator[](int i) const { data_[check(i)]; }
  453.             T& operator[](int i)       { data_[check(i)]; }
  454.       Array(const Array<T>&);
  455.       Array& operator= (const Array<T>&);
  456.     private:
  457.       int len_;
  458.       T*  data_;
  459.       int check(int i) const
  460.         { if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_);
  461.           return i; }
  462.     };
  463.  
  464. Unlike template functions, template classes (instantiations of class templates)
  465. need to be explicit about the parameters over which they are instantiating:
  466.  
  467.     main()
  468.     {
  469.       Array<int>           ai;
  470.       Array<float>         af;
  471.       Array<char*>         ac;
  472.       Array<String>        as;
  473.       Array< Array<int> >  aai;
  474.     }              // ^^^-- note the space; do NOT use "Array<Array<int>>"
  475.                    //       (the compiler sees ">>" as a single token).
  476.  
  477. ==============================================================================
  478.  
  479. Q120: What is a "parameterized type"?
  480.  
  481. Another way to say, "class templates."
  482.  
  483. A parameterized type is a type that is parameterized over another type or some
  484. value.  List<int> is a type ("List") parameterized over another type ("int").
  485.  
  486. ==============================================================================
  487.  
  488. Q121: What is "genericity"?
  489.  
  490. Yet another way to say, "class templates."
  491.  
  492. Not to be confused with "generality" (which just means avoiding solutions which
  493. are overly specific), "genericity" means class templates.
  494.  
  495. ==============================================================================
  496. SECTION 20: Libraries
  497. ==============================================================================
  498.  
  499. Q122: Where can I get a copy of "STL"?
  500.  
  501. "STL" is the "Standard Templates Library".  You can get a copy from:
  502.  
  503. STL HP official site:    ftp://butler.hpl.hp.com/stl
  504. STL code alternate:    ftp://ftp.cs.rpi.edu/stl
  505. STL code + examples:    http://www.cs.rpi.edu/~musser/stl.html
  506.  
  507. STL hacks for GCC-2.6.3 are part of the GNU libg++ package 2.6.2.1 or later
  508. (and they may be in an earlier version as well).  Thanks to Mike Lindner.
  509.  
  510. ==============================================================================
  511.  
  512. Q123: Where can I ftp the code that accompanies "Numerical Recipes"?
  513.  
  514. This software is sold and there for it would be illegal to provide it on the
  515. net.  However, its only about $30.
  516.  
  517. ==============================================================================
  518.  
  519. Q124: Why is my executable so large?
  520.  
  521. Many people are surprised by how big executables are, especially if the source
  522. code is trivial.  For example, a simple "hello world" program can generate an
  523. executable that is larger than most people expect (40+K bytes).
  524.  
  525. One reason executables can be large is that portions of the C++ runtime
  526. library gets linked with your program. How much gets linked in depends on how
  527. much of it you are using, and on how the implementor split up the library into
  528. pieces.  For example, the iostream library is quite large, and consists of
  529. numerous classes and virtual functions. Using any part of it might pull in
  530. nearly all of the iostream code as a result of the interdependencies.
  531.  
  532. You might be able to make your program smaller by using a dynamically-linked
  533. version of the library instead of the static version.
  534.  
  535. You have to consult your compiler manuals or the vendor's technical support
  536. for a more detailed answer.
  537.  
  538. ==============================================================================
  539. SECTION 21: Nuances of particular implementations
  540. ==============================================================================
  541.  
  542. Q125: GNU C++ (g++) produces big executables for tiny programs; Why?
  543.  
  544. libg++ (the library used by g++) was probably compiled with debug info (-g).
  545. On some machines, recompiling libg++ without debugging can save lots of disk
  546. space (~1 Meg; the down-side: you'll be unable to trace into libg++ calls).
  547. Merely "strip"ping the executable doesn't reclaim as much as recompiling
  548. without -g followed by subsequent "strip"ping the resultant "a.out"s.
  549.  
  550. Use "size a.out" to see how big the program code and data segments really are,
  551. rather than "ls -s a.out" which includes the symbol table.
  552.  
  553. ==============================================================================
  554.  
  555. Q126: Is there a yacc-able C++ grammar?
  556.  
  557. Jim Roskind is the author of a yacc grammar for C++. It's roughly compatible
  558. with the portion of the language implemented by USL cfront 2.0 (no templates,
  559. no exceptions, no run-time-type-identification).  Jim's grammar deviates from
  560. C++ in a couple of minor-but-subtle ways.
  561.  
  562. The grammar can be accessed by anonymous ftp from the following sites:
  563.  * ics.uci.edu (128.195.1.1) in "gnu/c++grammar2.0.tar.Z".
  564.  * mach1.npac.syr.edu (128.230.7.14) in "pub/C++/c++grammar2.0.tar.Z".
  565.  
  566. ==============================================================================
  567.  
  568. Q127: What is C++ 1.2?  2.0?  2.1?  3.0?
  569.  
  570. These are not versions of the language, but rather versions of cfront, which
  571. was the original C++ translator implemented by AT&T.  It has become generally
  572. accepted to use these version numbers as if they were versions of the language
  573. itself.
  574.  
  575. *VERY* roughly speaking, these are the major features:
  576.  * 2.0 includes multiple/virtual inheritance and pure virtual functions.
  577.  * 2.1 includes semi-nested classes and "delete [] ptr_to_array."
  578.  * 3.0 includes fully-nested classes, templates and "i++" vs "++i."
  579.  * 4.0 will include exceptions.
  580.  
  581. ==============================================================================
  582.  
  583. Q128: If name mangling was standardized, could I link code compiled with
  584.    compilers from different compiler vendors?
  585.  
  586. Short answer: Probably not.
  587.  
  588. In other words, some people would like to see name mangling standards
  589. incorporated into the proposed C++ ANSI standards in an attempt to avoiding
  590. having to purchase different versions of class libraries for different
  591. compiler vendors.  However name mangling differences are one of the smallest
  592. differences between implementations, even on the same platform.  Here is a
  593. partial list of other differences:
  594.  
  595. 1) Number and type of hidden arguments to member functions.
  596.    1a) is 'this' handled specially?
  597.    1b) where is the return-by-value pointer passed?
  598. 2) Assuming a vtable is used:
  599.    2a) what is its contents and layout?
  600.    2b) where/how is the adjustment to 'this' made for multiple inheritance?
  601. 3) How are classes laid out, including:
  602.    3a) location of base classes?
  603.    3b) handling of virtual base classes?
  604.    3c) location of vtable pointers, if vtables are used?
  605. 4) Calling convention for functions, including:
  606.    4a) does caller or callee adjust the stack?
  607.    4b) where are the actual parameters placed?
  608.    4c) in what order are the actual parameters passed?
  609.    4d) how are registers saved?
  610.    4e) where does the return value go?
  611.    4f) special rules for passing or returning structs or doubles?
  612.    4g) special rules for saving registers when calling leaf functions?
  613. 5) How is the run-time-type-identification laid out?
  614. 6) How does the runtime exception handling system know which local objects
  615.    need to be destructed during an exception throw?
  616.  
  617. ==============================================================================
  618. SECTION 22: Miscellaneous technical and environmental issues
  619. ==============================================================================
  620. SUBSECTION 22A: Miscellaneous technical issues:
  621. ==============================================================================
  622.  
  623. Q129: Why are classes with static data members getting linker errors?
  624.  
  625. Static data members must be explicitly defined in exactly one module.  E.g.,
  626.  
  627.     class Fred {
  628.     public:
  629.       //...
  630.     private:
  631.       static int i_;  //declares static data member "Fred::i_"
  632.       //...
  633.     };
  634.  
  635. The linker will holler at you ("Fred::i_ is not defined") unless you define (as
  636. opposed to declare) "Fred::i_" in (exactly) one of your source files:
  637.  
  638.     int Fred::i_ = some_expression_evaluating_to_an_int;
  639. or:
  640.     int Fred::i_;
  641.  
  642. The usual place to define static data members of class "Fred" is file "Fred.C"
  643. (or "Fred.cpp", etc; whatever filename extension you use).
  644.  
  645. ==============================================================================
  646.  
  647. Q130: What's the difference between the keywords struct and class?
  648.  
  649. The members and base classes of a struct are public by default, while in class,
  650. they default to private.  Note: you should make your base classes EXPLICITLY
  651. public, private, or protected, rather than relying on the defaults.
  652.  
  653. "struct" and "class" are otherwise functionally equivalent.
  654.  
  655. ==============================================================================
  656.  
  657. Q131: Why can't I overload a function by its return type?
  658.  
  659. If you declare both "char f()" and "float f()", the compiler gives you an error
  660. message, since calling simply "f()" would be ambiguous.
  661.  
  662. ==============================================================================
  663.  
  664. Q132: What is "persistence"?  What is a "persistent object"?
  665.  
  666. A persistent object can live after the program which created it has stopped.
  667. Persistent objects can even outlive different versions of the creating program,
  668. can outlive the disk system, the operating system, or even the hardware on
  669. which the OS was running when they were created.
  670.  
  671. The challenge with persistent objects is to effectively store their method code
  672. out on secondary storage along with their data bits (and the data bits and
  673. method code of all member objects, and of all their member objects and base
  674. classes, etc).  This is non-trivial when you have to do it yourself.  In C++,
  675. you have to do it yourself.  C++/OO databases can help hide the mechanism for
  676. all this.
  677.  
  678. ==============================================================================
  679.  
  680. Q133: Why is floating point so inaccurate?  Why doesn't this print 0.43?
  681.  
  682.     #include<iostream.h> 
  683.  
  684.     main()
  685.     {
  686.       float a = 1000.43;
  687.       float a = 1000.0;
  688.       cout << a - b << '\n';
  689.     } 
  690.  
  691. (note, on one C++ implementation, this prints 0.429993) 
  692.  
  693. Disclaimer: Frustration with rounding/truncation/approximation isn't really a
  694. C++ issue.  It's a computer science issue.  However, people keep asking about
  695. it on comp.lang.c++, so here's a nominal answer.
  696.  
  697. Answer: Floating point is an approximation.  The IEEE standard for 32 bit
  698. float supports 1 bit of sign, 8 bits of exponent, and 23 bits of mantissa.
  699. Since a normalized binary-point mantissa always has the form 1.xxxxx... the
  700. leading 1 is dropped and you get effectively 24 bits of mantissa.  The number
  701. 1000.43 (and many, many others) is not exactly representable in float or
  702. double format.  1000.43 is actually represented as the following bitpattern
  703. (the 's' shows the position of the sign bit, the 'e's show the positions of
  704. the exponent bits, and the 'm's show the positions of the mantissa bits):
  705.  
  706.     seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm 
  707.     01000100011110100001101110000101 
  708.  
  709. The shifted mantissa is 1111101000.01101110000101 or 1000 + 7045/16384.  The
  710. fractional part is 0.429992675781.  With 24 bits of mantissa you only get
  711. about 1 part in 16M of precision for float.  The 'double' type provides more
  712. precision (53 bits of mantissa).
  713.  
  714. ==============================================================================
  715. SUBSECTION 22B: Miscellaneous environmental issues:
  716. ==============================================================================
  717.  
  718. Q134: Is there a TeX or LaTeX macro that fixes the spacing on "C++"?
  719.  
  720. Yes, here are two:
  721.  
  722. \def\CC{C\raise.22ex\hbox{{\footnotesize +}}\raise.22ex\hbox{\footnotesize +}}
  723.  
  724. \def\CC{{C\hspace{-.05em}\raisebox{.4ex}{\tiny\bf ++}}}
  725.  
  726. ==============================================================================
  727.  
  728. Q135: Where can I access C++2LaTeX, a LaTeX pretty printer for C++ source?
  729.  
  730. Here are a few ftp locations:
  731.  
  732. Host aix370.rrz.uni-koeln.de   (134.95.80.1) Last updated 15:41 26 Apr 1991
  733.     Location: /tex
  734.       FILE      rw-rw-r--     59855  May  5  1990   C++2LaTeX-1.1.tar.Z
  735. Host utsun.s.u-tokyo.ac.jp   (133.11.11.11) Last updated 05:06 20 Apr 1991
  736.     Location: /TeX/macros
  737.       FILE      rw-r--r--     59855  Mar  4 08:16   C++2LaTeX-1.1.tar.Z
  738. Host nuri.inria.fr   (128.93.1.26) Last updated 05:23  9 Apr 1991
  739.     Location: /TeX/tools
  740.       FILE      rw-rw-r--     59855  Oct 23 16:05   C++2LaTeX-1.1.tar.Z
  741. Host iamsun.unibe.ch   (130.92.64.10) Last updated 05:06  4 Apr 1991
  742.     Location: /TeX
  743.       FILE      rw-r--r--     59855  Apr 25  1990   C++2LaTeX-1.1.tar.Z
  744. Host iamsun.unibe.ch   (130.92.64.10) Last updated 05:06  4 Apr 1991
  745.     Location: /TeX
  746.       FILE      rw-r--r--     51737  Apr 30  1990
  747.       C++2LaTeX-1.1-PL1.tar.Z
  748. Host tupac-amaru.informatik.rwth-aachen.de   (192.35.229.9) Last updated 05:07 18 Apr 1991
  749.     Location: /pub/textproc/TeX
  750.       FILE      rw-r--r--     72957  Oct 25 13:51  C++2LaTeX-1.1-PL4.tar.Z
  751. Host wuarchive.wustl.edu   (128.252.135.4) Last updated 23:25 30 Apr 1991
  752.     Location: /packages/tex/tex/192.35.229.9/textproc/TeX
  753.       FILE      rw-rw-r--     49104  Apr 10  1990   C++2LaTeX-PL2.tar.Z
  754.       FILE      rw-rw-r--     25835  Apr 10  1990   C++2LaTeX.tar.Z
  755. Host tupac-amaru.informatik.rwth-aachen.de   (192.35.229.9) Last updated 05:07 18 Apr 1991
  756.     Location: /pub/textproc/TeX
  757.       FILE rw-r--r-- 74015  Mar 22 16:23 C++2LaTeX-1.1-PL5.tar.Z
  758.     Location: /pub
  759.       FILE rw-r--r-- 74015  Mar 22 16:23 C++2LaTeX-1.1-PL5.tar.Z
  760. Host sol.cs.ruu.nl   (131.211.80.5) Last updated 05:10 15 Apr 1991
  761.     Location: /TEX/TOOLS
  762.       FILE      rw-r--r--     74015  Apr  4 21:02x   C++2LaTeX-1.1-PL5.tar.Z
  763. Host tupac-amaru.informatik.rwth-aachen.de (192.35.229.9) Last updated 05:07 18 Apr 1991
  764.     Location: /pub/textproc/TeX
  765.       FILE      rw-r--r--      4792  Sep 11  1990 C++2LaTeX-1.1-patch#1
  766.       FILE      rw-r--r--      2385  Sep 11  1990 C++2LaTeX-1.1-patch#2
  767.       FILE      rw-r--r--      5069  Sep 11  1990 C++2LaTeX-1.1-patch#3
  768.       FILE      rw-r--r--      1587  Oct 25 13:58 C++2LaTeX-1.1-patch#4
  769.       FILE      rw-r--r--      8869  Mar 22 16:23 C++2LaTeX-1.1-patch#5
  770.       FILE      rw-r--r--      1869  Mar 22 16:23 C++2LaTeX.README
  771. Host rusmv1.rus.uni-stuttgart.de   (129.69.1.12) Last updated 05:13 13 Apr 1991
  772.     Location: /soft/tex/utilities
  773.       FILE      rw-rw-r--    163840  Jul 16  1990   C++2LaTeX-1.1.tar
  774.  
  775. ==============================================================================
  776.  
  777. Q136: Where can I access "tgrind," a pretty printer for C++/C/etc source?
  778.  
  779. "tgrind" reads a C++ source file, and spits out something that looks pretty on
  780. most Unix printers.  It usually comes with the public distribution of TeX and
  781. LaTeX; look in the directory: "...tex82/contrib/van/tgrind".  A more up-to-date
  782. version of tgrind by Jerry Leichter can be found on: venus.ycc.yale.edu in
  783. [.TGRIND].
  784.  
  785. ==============================================================================
  786.  
  787. Q137: Is there a C++-mode for GNU emacs?  If so, where can I get it?
  788.  
  789. Yes, there is a C++-mode for GNU emacs.
  790.  
  791. The latest and greatest version of C++-mode (and c-mode) is implemented in the
  792. file cc-mode.el.  It is an extension of Detlef & Clamen's version.
  793. A version is included with emacs.  Newer version are availiable from
  794. the elisp archives.
  795.  
  796. ==============================================================================
  797.  
  798. Q138: Where can I get OS-specific FAQs answered (e.g.,BC++,DOS,Windows,etc)?
  799.  
  800. See one of the following:
  801.  * comp.os.msdos.programmer
  802.  * comp.windows.ms.programmer
  803.  * comp.unix.programmer
  804.  
  805. [If anyone has an email address for a BC++, VC++, or Semantic C++ bug list
  806. and/or discussion mailing list, please let me know how to subscribe, and I'll
  807. mention it here].
  808.  
  809. ==============================================================================
  810.  
  811. Q139: Why does my DOS C++ program says "Sorry: floating point code not
  812.    linked"?
  813.  
  814. The compiler attempts to save space in the executable by not including the
  815. float-to-string format conversion routines unless they are necessary, but
  816. sometimes it guesses wrong, and gives you the above error message.  You can fix
  817. this by (1) using <iostream.h> instead of <stdio.h>, or (2) by including the
  818. following function somewhere in your compilation (but don't call it!):
  819.  
  820.     static void dummyfloat(float *x) { float y; dummyfloat(&y); }
  821.  
  822. See FAQ on stream I/O for more reasons to use <iostream.h> vs <stdio.h>.
  823.  
  824. ==============================================================================
  825.  
  826. Q140: Why does my BC++ Windows app crash when I'm not running the BC45 IDE?
  827.  
  828. If you're using BC++ for a Windows app, and it works ok as long as you have the
  829. BC45 IDE running, but when the BC45 IDE is shut down you get an exception
  830. during the creation of a window, then add the following line of code to the
  831. InitMainWindow() method of your application ("YourApp::InitMainWindow()"):
  832.  
  833.     EnableBWCC(TRUE);
  834.  
  835. ==============================================================================
  836.  
  837. --
  838. Paradigm Shift, Inc. / P.O. Box 5108 / Potsdam, NY  13676
  839. Technology consulting services
  840. cline@parashift.com / Voice: 315-353-6100 / FAX: 315-353-6110
  841.